var cacheFiles = [
  '5BB072CAB1104C1F2E254F7F0EC0EB70.jpg',
  'main.js'
]
self.addEventListener('install', function (evt) {
  evt.waitUntil(
    caches.open('my-test-cahce-v1').then(function (cache) {
      return cache.addAll(cacheFiles)
    })
  )
})

// 缓存图片
self.addEventListener('fetch', function (evt) {
  console.log('service worker fetch 捕捉', evt)
  evt.respondWith(
    caches.match(evt.request).then(function (response) {
      if (response) {
        console.log('拦截了', response)
        return response
      }
      var request = evt.request.clone()
      return fetch(request).then(function (response) {
        if (!response && response.status !== 200 && !response.headers.get('Content-type').match(/image/)) {
          console.log('请求没有拦截了', response)
          return response
        }
        var responseClone = response.clone()
        caches.open('my-test-cache-v1').then(function (cache) {
          cache.put(evt.request, responseClone)
        })
        return response
      })
    })
  )
})
